package com.cloudfast.platform.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloudfast.common.result.ApiResponse;
import com.cloudfast.common.user.LoginUser;
import com.cloudfast.common.user.UserUtils;
import com.cloudfast.email.JEmailSender;
import com.cloudfast.email.text.EmailMessage;
import com.cloudfast.platform.menu.entity.Menu;
import com.cloudfast.platform.menu.service.MenuService;
import com.cloudfast.platform.role.entity.Role;
import com.cloudfast.platform.role.service.RoleService;
import com.cloudfast.platform.user.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RequestMapping(value = "/cloud")
@Api(value = "公共接口", tags = "公共接口")
@RestController
public class CommonController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserService userService;

    @Autowired
    private JEmailSender jEmailSender;

    /**
     * 前端获取当前用户及权限信息
     *
     * @return
     */
    @PostMapping(value = "/user")
    @ApiOperation("获取当前用户及权限")
    public ApiResponse loginUser() {
        Map<String, Object> map = new HashMap<>();
        map.put("loginUser", UserUtils.getUser());
        map.put("access", UserUtils.getPermission());
        map.put("screenBtn", UserUtils.getUserScreenButtons());
        map.put("platformMenus", UserUtils.getUserMenus("System"));
        map.put("portalMenus", UserUtils.getUserMenus("Portal"));
        return ApiResponse.success(map);
    }

    /**
     * @param roleId
     * @param userId
     * @return
     */
    @PostMapping(value = "addUserRole")
    @ApiOperation("用户授权角色")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true)})
    public ApiResponse addUserRole(Long roleId, Long userId) {
        roleService.addUser(roleId, userId);
        return ApiResponse.success(true);
    }

    @PostMapping(value = "moveUserRole")
    @ApiOperation("取消用户角色")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true)})
    public ApiResponse moveUserRole(Long roleId, Long userId) {
        roleService.moveUser(roleId, userId);
        return ApiResponse.success(true);
    }

    @PostMapping(value = "/oauth/menus")
    @ApiOperation("查询用户 下当前模式(permissionScope)下的菜单 ")
    @ApiImplicitParams({@ApiImplicitParam(name = "permissionScope", value = "权限域", required = true), @ApiImplicitParam(name = "userId", value = "用户id", required = true)})
    public List<Map<String, Object>> menus(String permissionScope, Long userId) {
        Menu menu = this.menuService.findTopMenuByScope(permissionScope);
        return menuService.findUserHasMenu(menu.getId(), userId);
    }

    @PostMapping(value = "/oauth/screenButtons")
    @ApiOperation("查询用户 下当前模式(permissionScope)下的菜单 ")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", required = true)})
    public List<Menu> screenButtons(Long userId) {
        return menuService.findScreenBtn(userId);
    }

    /**
     * 根据用户名称查询用户
     *
     * @param loginName
     * @return
     */
    @PostMapping(value = "/oauth/findByLoginName")
    @ApiOperation("根据登入名称或手机查询用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "loginName", value = "用户用户名或电话", required = true)})
    public LoginUser findByLoginName(String loginName) {
        return this.userService.findByLoginName(loginName);
    }

    @PostMapping(value = "/oauth/findRoleCodeByUserId")
    @ApiOperation("根据登入用户查询用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", required = true)})
    public List<Role> findRoleCodeByUserId(Long userId) {
        return this.roleService.findRoleByUserId(userId);
    }

    @PostMapping(value = "/oauth/findPermissionByUserId")
    @ApiOperation("根据登入用户查询权限")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", required = true)})
    public List<Menu> findPermissionByUserId(Long userId) {
        return this.menuService.findPermissionByUserId(userId);
    }

    @PostMapping(value = "/oauth/sendEmail")
    @ApiOperation("发送邮箱")
    @ApiImplicitParams({@ApiImplicitParam(name = "revUser", value = "接收人", required = true)})
    public void sendEmail(String revUser, String content) {
        EmailMessage emailMessage = new EmailMessage();
        emailMessage.setContent(content);
        emailMessage.setTitle("测试");
        emailMessage.setRevUser(revUser);
        jEmailSender.sendTextMail(emailMessage);
    }

}
